package cn.edu.nju.newsFeed;

import java.util.ArrayList;

import cn.edu.nju.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageViewActivity extends Activity implements ViewFactory,OnTouchListener,OnGestureListener{
	private NewsFeedItem feedItem;
	private int currentImage = 0; // the index of current image
	private int imageNumber;
	
	private GestureDetector mGestureDetector;
	private ImageSwitcher is;
	private ArrayList<Bitmap> images = new ArrayList<Bitmap>();
	
	private TextView textView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_viewer);
		
		Intent intent = getIntent();
		int position = intent.getIntExtra(NewsFeedActivity.FEED_ITEM_POSITION, 0);
		feedItem = NewsFeedProvider.getInstance(this).getNewsFeedItem(position);
		imageNumber = feedItem.getImageNumber();
		
		is = (ImageSwitcher)findViewById(R.id.imageSwitcher);
		is.setFactory(this);
		is.setOnTouchListener(this);
		is.setLongClickable(true);
		
		mGestureDetector = new GestureDetector(this);
		
		textView = (TextView)findViewById(R.id.textView_num);
	}

	private void showPreviousImage(){
		is.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_in));
		is.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
		
		if(currentImage == 0){
			return;
		} else {
			currentImage--;
			setImageView();
		}
	}
	
	private void showNextImage(){
		is.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
		is.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
		
		if(currentImage == (imageNumber - 1)){
			return;
		}
		
		currentImage++;
		if(currentImage == images.size()){
			images.add(feedItem.getImage(currentImage));
		}
		
		setImageView();
	}
	
	private void setImageView(){
		Bitmap bitmap = images.get(currentImage);
		is.setImageDrawable(new BitmapDrawable(getResources(), bitmap));
		
		textView.setText(currentImage + 1 +  "/" + imageNumber);
		
		Display display = getWindowManager().getDefaultDisplay();
		LayoutParams layoutParams = getWindow().getAttributes();
		layoutParams.width = display.getWidth();
		layoutParams.height = display.getHeight();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Bitmap image = feedItem.getImage(currentImage);
		images.add(image);
		setImageView();
	}

	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	public View makeView() {
		ImageView i = new ImageView(this);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		return i;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		
		if(e2.getX() - e1.getX() > 50){
			showPreviousImage();
		} else if(e1.getX() - e2.getX() > 50){
			showNextImage();
		}
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}
